package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

public class TCPServer {
	/**
	 * @param args
	 * @throws SocketException 
	 */

	public static void main(String[] args) throws IOException {
		String request;							// Message received by the server.
		String reply = "Received";				// Message confirming reception of messge from client.
		int serverPort = Integer.parseInt(args[0]);
		
		ServerSocket srvSocket = new ServerSocket(serverPort);	// Create server side socket.
		
		while(true) {
			Socket clientSocket = srvSocket.accept();
			// Optionally create a thread for each socket.
			BufferedReader sin = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
			PrintWriter sout = new PrintWriter(clientSocket.getOutputStream(), true);
			
			request = sin.readLine();
			System.out.println(request + "\n");
			
			sout.println(reply + ": " + request);
			
			clientSocket.close();
		}
	}
}
